package com.js.data.dtmg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.js.data.common.constant.SymbolConstant;
import com.js.data.common.dto.SecurityUserDTO;
import com.js.data.common.exception.ApiException;
import com.js.data.common.utils.ListUtil;
import com.js.data.common.utils.SecurityUtils;
import com.js.data.dtmg.datasource.DataSourceConfig;
import com.js.data.dtmg.dto.DtDatasourceDto;
import com.js.data.dtmg.dto.DtSourceQuery;
import com.js.data.dtmg.entity.DtDatasource;
import com.js.data.dtmg.mapper.DtDatasourceMapper;
import com.js.data.dtmg.service.DtDatasourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.js.data.dtmg.util.DbUtil;
import com.js.data.dtmg.vo.DtDatasourceVO;
import com.js.data.system.query.UserQuery;
import com.js.data.system.vo.UserPageVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

/**
 * <p>
 * 数据源表 服务实现类
 * </p>
 *
 * @author hh
 * @since 2024-07-16
 */
@Service
public class DtDatasourceServiceImpl extends ServiceImpl<DtDatasourceMapper, DtDatasource> implements DtDatasourceService {
    /**
     * 用户分页
     *
     * @param query 查询条件
     * @return IPage<UserPageVO>
     */
    @Override
    public IPage<DtDatasourceVO> pageList(DtSourceQuery query) {
        Page<DtDatasource> inPage = new Page<>(query.getCurrent(), query.getSize());
        LambdaQueryWrapper<DtDatasource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(query.getDtName()),DtDatasource::getDtName,query.getDtName());

        page(inPage,queryWrapper);
        IPage<DtDatasourceVO> retPage = new Page<>();
        BeanUtils.copyProperties(inPage, retPage);
        List<DtDatasource> records =  inPage.getRecords();
        retPage.setRecords(ListUtil.copyListToList(records,DtDatasourceVO.class));
        return retPage;
    }
    /**
     * 新增
     *
     * @param dto 表单
     */
    @Override
    public void save(DtDatasourceDto dto) {
        DtDatasource dtDatasource = new DtDatasource();
        BeanUtils.copyProperties(dto, dtDatasource);
        saveOrUpdate(dtDatasource);
    }

    /**
     * 详情
     *
     * @param id      事项id
     */
    @Override
    public DtDatasourceVO detail(Long id){
        DtDatasource item =  getById(id);
        DtDatasourceVO vo = new DtDatasourceVO();
        BeanUtils.copyProperties(item,vo);
        return  vo;
    }
    /**
     * 删除
     *
     * @param id id
     */
    @Override
    public void delete(Long id) {
        DtDatasource dtDatasource = getById(id);
        Optional.ofNullable(dtDatasource).orElseThrow(() -> new ApiException("数据源不存在"));
        if (!removeById(id)) {
            throw new ApiException("删除数据源失败");
        }
    }

    /**
     * 测试数据源连通性
     * @param dto 数据
     */
    @Override
    public void testOk(DtDatasourceDto dto){
        DataSource dataSource = DataSourceConfig.getDataSource(dto);
        Connection conn = null;
        try {
            conn =  dataSource.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
    @Override
    public List<DtDatasource> selectList(){
        LambdaQueryWrapper<DtDatasource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(DtDatasource::getId,DtDatasource::getDtName);
        return list(queryWrapper);
    }

    /**
     * 查询数据库表信息
     * @param id 数据源ID
     * @return 表信息
     */
    @Override
    public List<Map<String,String>> getDbTables(Long id) {
        return DbUtil.getDbTables(id);
    }
}
